/**
 * Copyright (C) 2021 - 2031 O-Cubes Co., Ltd.
 */
/****************************************************************
 *  @file    arcdetect.h
 *  @brief   ArcDetect header file
 *  @version v1.0
 *  @date    14. Dec. 2023
 ****************************************************************/
#ifndef __ARCDETECT_H__
#define __ARCDETECT_H__

#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif

#define MONITOR_CHANNEL_NUM	14
#define SAV_POINTS_PERCYCLE	128
#define MAXBIT_FFT		7
#define MAX_SAV_POINTS		384//640
#define CD1_POINTS_PERCYCLE	64
#define CD2_POINTS_PERCYCLE	32
#define CD3_POINTS_PERCYCLE	16
#define ARC_UNIT_NUM		3
#define ZEROPOINTS_NUM		8
#define HALFCYLCLE_NUM		6
#define RESULT_BUFLEN		100
#define CD1_LENGTH		64
#define CD2_LENGTH		32
#define CD3_LENGTH		16
#define myabs(x)		(x>0?x:-(x))

typedef enum arctype{
	arc0 = 0,
	arc1,
	arc2,
	arcnum
}ARC_TYPE;

typedef struct 		//¹ýÁã¼ì²â
{
	int16_t		SmpData0;
	int16_t		SmpData1;
	int16_t		SmpData2;
	int16_t		SmpData3;
}TZeroCheck;

typedef struct sqrt16_t
{
	uint16_t	root;
	uint16_t	rem;
}SQRT16_T;

typedef struct
{
	//ÊäÈë
	//uint16_t	*pSav;
	uint32_t	process_count;
	
	//Êä³ö
	uint8_t		TCA_arc_fault[ARC_UNIT_NUM][RESULT_BUFLEN];//TCA½á¹û
	uint8_t		TCA_arc_idx[ARC_UNIT_NUM];
	uint8_t		TBL_arc_fault[ARC_UNIT_NUM][RESULT_BUFLEN];//²îÖµ¾ù·½¸ù½á¹û
	uint8_t		TBL_arc_idx[ARC_UNIT_NUM];
	uint8_t		CD1_arc_fault[ARC_UNIT_NUM][RESULT_BUFLEN];//CD1¾ùÖµ/·½²î/Âö³å/ÄÜÁ¿½á¹û
	uint8_t		CD1_arc_idx[ARC_UNIT_NUM];
	uint8_t		PJLX_arc_fault[ARC_UNIT_NUM][RESULT_BUFLEN];//Æ½¼çÁãÐÝ½á¹û
	uint8_t		PJLX_arc_idx[ARC_UNIT_NUM];
	uint8_t		DIDT_arc_fault[ARC_UNIT_NUM][RESULT_BUFLEN];//µçÁ÷ÉÏÉýÂÊ½á¹û
	uint8_t		DIDT_arc_idx[ARC_UNIT_NUM];
	
	//±äÁ¿
	TZeroCheck	Uchn[ARC_UNIT_NUM];
	uint32_t	tca_setvalue;
	uint32_t	tbl_setvalue;
	uint32_t	del_setvalue;
	uint32_t	CD1_avr_setvalue;	//cD1¾ùÖµÃÅ¼÷
	uint32_t	CD1_pls_setvalue;	//cD1Âö³åÃÅ¼÷
	uint32_t	CD1_num_setvalue;	//cD1Âö³å¸öÊý
	uint32_t	CD1_var_setvalue;	//cD1·½²îÃÅ¼÷
	uint32_t	CD1_enr_setvalue;	//cD1ÄÜÁ¿ÃÅ¼÷
	uint32_t	DIDT_setvalue;		//di/dtÃÅ¼÷
	uint32_t	shoulder_setvalue;	//Æ½¼ç²¿ÃÅ¼÷
	uint32_t	zeronum_setvalue;	//ÁãÐÝµãÊýÃÅ¼÷
	uint32_t	Iarc_halfcycle_abs[ARC_UNIT_NUM][HALFCYLCLE_NUM];
	uint32_t	Iarc_fullcycle_abs[ARC_UNIT_NUM][HALFCYLCLE_NUM/2];
	uint32_t	Iarc_deltcycle_abs[ARC_UNIT_NUM];
	uint16_t	zero_point_idx[ARC_UNIT_NUM][ZEROPOINTS_NUM];
	uint16_t	cur_smv_idx[ARC_UNIT_NUM];
	uint16_t	pre_smv_idx[ARC_UNIT_NUM];
	uint8_t		new_smv_num[ARC_UNIT_NUM];
	uint8_t		zero_buf_idx[ARC_UNIT_NUM];
	uint8_t		Iarc_halfcycle_idx[ARC_UNIT_NUM];
	int16_t		Iarc_smv_buf[ARC_UNIT_NUM][SAV_POINTS_PERCYCLE*2];//²ÉÑùÖµ»º´æ(Ò»ÖÜ²¨Ë«·Ý)
	uint8_t		Iarc_smv_idx[ARC_UNIT_NUM];
	int16_t		Iarc_tbl_smv[ARC_UNIT_NUM][SAV_POINTS_PERCYCLE];//Í»±äÁ¿²îÖµ
	uint8_t		Iarc_tbl_idx[ARC_UNIT_NUM];
	int16_t		Iarc_diff_smv[ARC_UNIT_NUM][SAV_POINTS_PERCYCLE];//µçÁ÷²î·ÖÖµ£¨ÉÏÉýÂÊ£©
	uint8_t		Iarc_diff_idx[ARC_UNIT_NUM];
	uint32_t	Iarc_tbl_sqs[ARC_UNIT_NUM];	//sqs---smv squre sum
	uint32_t	Iarc_tbl_rms[ARC_UNIT_NUM][3];
	uint8_t		Iarc_rms_idx[ARC_UNIT_NUM];
	uint32_t	Iarc_rms_cur[ARC_UNIT_NUM];
	uint32_t	Iarc_rms_del[ARC_UNIT_NUM];
	uint16_t	cur_cD1_idx[ARC_UNIT_NUM];
	uint16_t	pre_cD1_idx[ARC_UNIT_NUM];
	int16_t		Iarc_cD1_val[ARC_UNIT_NUM][CD1_POINTS_PERCYCLE];
	uint8_t		Iarc_cD1_idx[ARC_UNIT_NUM];
	int32_t		Iarc_cD1_avr[ARC_UNIT_NUM];	//¾ùÖµ
	uint32_t	Iarc_cD1_pulsenum[ARC_UNIT_NUM];//Âö³å¸öÊý
	uint32_t	Iarc_cD1_sqs[ARC_UNIT_NUM];	//²îÖµÆ½·½ºÍ
	uint32_t	Iarc_cD1_var[ARC_UNIT_NUM][3];	//·½²îvariance
	uint8_t		Iarc_var_idx[ARC_UNIT_NUM];
	uint32_t	Iarc_cD1_enr[ARC_UNIT_NUM][3];	//ÄÜÁ¿energy
	uint8_t		Iarc_enr_idx[ARC_UNIT_NUM];
	uint32_t	Iarc_zero_num[ARC_UNIT_NUM];
	uint32_t	Iarc_diff_maxval[ARC_UNIT_NUM];	//DIDT×î´óÖµ
	
	int32_t		cD1[ARC_UNIT_NUM][CD1_LENGTH];
	int32_t		cA1[ARC_UNIT_NUM][CD1_LENGTH*2];
	int32_t		cD2[ARC_UNIT_NUM][CD2_LENGTH];
	int32_t		cA2[ARC_UNIT_NUM][CD2_LENGTH*2];
	int32_t		cD3[ARC_UNIT_NUM][CD3_LENGTH];
	int32_t		cA3[ARC_UNIT_NUM][CD3_LENGTH];
	uint8_t		first_dec_index[ARC_UNIT_NUM];	//Ò»¼¶Ð¡²¨·Ö½âË÷Òý
	uint8_t		secnd_dec_index[ARC_UNIT_NUM];	//¶þ¼¶Ð¡²¨·Ö½âË÷Òý
	uint8_t		third_dec_index[ARC_UNIT_NUM];	//Èý¼¶Ð¡²¨·Ö½âË÷Òý
	
	int32_t		cD1_R[ARC_UNIT_NUM][SAV_POINTS_PERCYCLE];	//cD1ÖØ¹¹ÐÅºÅ
	int32_t		cD2_R[ARC_UNIT_NUM][SAV_POINTS_PERCYCLE];	//cD2ÖØ¹¹ÐÅºÅ
	int32_t		cD3_R[ARC_UNIT_NUM][SAV_POINTS_PERCYCLE];	//cD3ÖØ¹¹ÐÅºÅ
	int32_t		cA3_R[ARC_UNIT_NUM][SAV_POINTS_PERCYCLE];	//cA3ÖØ¹¹ÐÅºÅ
	float		EcD1_R[ARC_UNIT_NUM];	//cD1ÖØ¹¹ÐÅºÅÄÜÁ¿
	float		EcD2_R[ARC_UNIT_NUM];	//cD2ÖØ¹¹ÐÅºÅÄÜÁ¿
	float		EcD3_R[ARC_UNIT_NUM];	//cD3ÖØ¹¹ÐÅºÅÄÜÁ¿
	float		EcA3_R[ARC_UNIT_NUM];	//cA3ÖØ¹¹ÐÅºÅÄÜÁ¿
	float		EcDT_R[ARC_UNIT_NUM];	//cD1~cD3+cA3ÖØ¹¹ÐÅºÅ×ÜÄÜÁ¿
	float		pEcD[ARC_UNIT_NUM][4];	//cD1ÖØ¹¹ÐÅºÅÄÜÁ¿Õ¼±È
	float		Entropy[ARC_UNIT_NUM];	//cD1~cD3+cA3ÖØ¹¹ÐÅºÅÄÜÆ×ìØ
	
	int16_t		Isar_smv_buf[5][SAV_POINTS_PERCYCLE];	//²ÉÑùÖµ»º´æ(ARC_IA,ARC_IB,ARC_IC,IL,UA)
	
}ARCDETECT;

static const uint16_t SYS_acos[290]=
{
    	9000,
    	8986,
    	8972,
    	8958,
    	8944,
    	8930,
    	8916,
    	8902,
    	8888,
    	8874,
    	8860,
    	8846,
    	8832,
    	8818,
    	8804,
    	8790,
    	8776,
    	8762,
    	8748,
    	8734,
    	8720,
    	8706,
    	8692,
    	8678,
    	8664,
    	8650,
    	8636,
    	8622,
    	8608,
    	8594,
    	8580,
    	8566,
    	8552,
    	8538,
    	8524,
    	8510,
    	8496,
    	8482,
    	8468,
    	8454,
    	8440,
    	8426,
    	8411,
    	8397,
    	8383,
    	8369,
    	8355,
    	8341,
    	8327,
    	8313,
    	8299,
    	8285,
    	8271,
    	8257,
    	8242,
    	8228,
    	8214,
    	8200,
    	8186,
    	8172,
    	8158,
    	8144,
    	8129,
    	8115,
    	8101,
    	8087,
    	8073,
    	8059,
    	8044,
    	8030,
    	8016,
    	8002,
    	7988,
    	7973,
    	7959,
    	7945,
    	7931,
    	7916,
    	7902,
    	7888,
    	7874,
    	7859,
    	7845,
    	7831,
    	7817,
    	7802,
    	7788,
    	7774,
    	7759,
    	7745,
    	7731,
    	7716,
    	7702,
    	7688,
    	7673,
    	7659,
    	7645,
    	7630,
    	7616,
    	7601,
    	7587,
    	7572,
    	7558,
    	7544,
    	7529,
    	7515,
    	7500,
    	7486,
    	7471,
    	7457,
    	7442,
    	7428,
    	7413,
    	7399,
    	7384,
    	7369,
    	7355,
    	7340,
    	7326,
    	7311,
    	7296,
    	7282,
    	7267,
    	7252,
    	7238,
    	7223,
    	7208,
    	7194,
    	7179,
    	7164,
    	7150,
    	7135,
    	7120,
    	7105,
    	7090,
    	7076,
    	7061,
    	7046,
    	7031,
    	7016,
    	7001,
    	6986,
    	6972,
    	6957,
    	6942,
    	6927,
    	6912,
    	6897,
    	6882,
    	6867,
    	6852,
    	6837,
    	6822,
    	6807,
    	6792,
    	6776,
    	6761,
    	6746,
    	6731,
    	6716,
    	6701,
    	6685,
    	6670,
    	6655,
    	6640,
    	6624,
    	6609,
    	6594,
    	6579,
    	6563,
    	6548,
    	6532,
    	6517,
    	6502,
    	6486,
    	6471,
    	6455,
    	6440,
    	6424,
    	6409,
    	6393,
    	6378,
    	6362,
    	6346,
    	6331,
    	6315,
    	6299,
    	6284,
    	6268,
    	6252,
    	6236,
    	6221,
    	6205,
    	6189,
    	6173,
    	6157,
    	6141,
    	6125,
    	6109,
    	6093,
    	6077,
    	6061,
    	6045,
    	6029,
    	6013,
    	5997,
    	5981,
    	5964,
    	5948,
    	5932,
    	5916,
    	5899,
    	5883,
    	5867,
    	5850,
    	5834,
    	5817,
    	5801,
    	5784,
    	5768,
    	5751,
    	5735,
    	5718,
    	5701,
    	5685,
    	5668,
    	5651,
    	5634,
    	5618,
    	5601,
    	5584,
    	5567,
    	5550,
    	5533,
    	5516,
    	5499,
    	5482,
    	5465,
    	5448,
    	5430,
    	5413,
    	5396,
    	5378,
    	5361,
    	5344,
    	5326,
    	5309,
    	5291,
    	5274,
    	5256,
    	5239,
    	5221,
    	5203,
    	5185,
    	5168,
    	5150,
    	5132,
    	5114,
    	5096,
    	5078,
    	5060,
    	5042,
    	5023,
    	5005,
    	4987,
    	4969,
    	4950,
    	4932,
    	4913,
    	4895,
    	4876,
    	4858,
    	4839,
    	4820,
    	4801,
    	4783,
    	4764,
    	4745,
    	4726,
    	4707,
    	4687,
    	4668,
    	4649,
    	4630,
    	4610,
    	4591,
    	4571,
    	4552,
    	4532,
    	4512
};

extern int16_t gSav[MONITOR_CHANNEL_NUM][MAX_SAV_POINTS];
extern uint32_t gSavIdx[MONITOR_CHANNEL_NUM];
extern float iWr[SAV_POINTS_PERCYCLE];
extern float iWx[SAV_POINTS_PERCYCLE];
extern uint16_t bit_rev[SAV_POINTS_PERCYCLE];

extern void *NewArcDetect(ARCDETECT *dp);
extern void RunArcDetect(ARCDETECT *dp);
extern SQRT16_T SYS_sqrt32(uint32_t val);
extern uint32_t SYS_fabSqrt32(int32_t ar, int32_t bx);
extern void kkfft(float *fr, float *fi, float *fTemp, uint32_t n, uint32_t k);
extern void WaveletDwt(int16_t *sourceData, int16_t dataLen, int32_t *cA, int32_t *cD);
extern void getcD1(int32_t *cD, int32_t recLen, int32_t *cD_R);
extern void getcD2(int32_t *cD, int32_t recLen, int32_t *cD_R);
extern void getcD3(int32_t *cD, int32_t recLen, int32_t *cD_R);
extern void getcA3(int32_t *cA, int32_t recLen, int32_t *cA_R);
#ifdef __cplusplus
}
#endif

#endif /* __ARCDETECT_H__ */

